package cn.stylefeng.guns.modular.weixin.controller;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.*;
import org.springframework.beans.factory.annotation.*;
import me.chanjar.weixin.common.error.*;
import org.springframework.web.context.request.*;
import me.chanjar.weixin.common.bean.menu.*;
import java.net.*;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.modular.weixin.utils.*;
import org.apache.commons.io.*;
import java.io.*;
import me.chanjar.weixin.mp.bean.menu.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping({ "/wxmp/menu/{appid}" })
public class WxMenuController
{
    private WxMpService wxService;
    
    @Autowired
    public WxMenuController(final WxMpService wxService) {
        this.wxService = wxService;
    }
    
    @PostMapping({ "/create" })
    public String menuCreate(@PathVariable final String appid, @RequestBody final WxMenu menu) throws WxErrorException {
        return this.wxService.switchoverTo(appid).getMenuService().menuCreate(menu);
    }
    
    @GetMapping({ "/create" })
    public String menuCreateSample(@PathVariable final String appid) throws WxErrorException, MalformedURLException {
        final WxMenu menu = new WxMenu();
        final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        WxMenuButton userCenterButton = new WxMenuButton();
        userCenterButton.setType(WxConsts.MenuButtonType.CLICK);
        userCenterButton.setName("个人中心");
        menu.getButtons().add(userCenterButton);

        WxMenuButton userButton = new WxMenuButton();
        userButton.setType(WxConsts.MenuButtonType.CLICK);
        userButton.setKey("Q1001_userinfo");
        userButton.setName("个人查询");
        userCenterButton.getSubButtons().add(userButton);
        WxMenuButton queryButton = new WxMenuButton();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        URL requestURL = new URL(request.getRequestURL().toString());
        if (servletRequestAttributes != null) {

            boolean switchover = this.wxService.switchover(appid);
            String url = wxService.oauth2buildAuthorizationUrl(
                    String.format("%s://%s/app/toBind", requestURL.getProtocol(), requestURL.getHost(), appid),
                    WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
            queryButton.setUrl(url);
        }
        queryButton.setType(WxConsts.MenuButtonType.VIEW);
        queryButton.setName("卡号绑定");
        userCenterButton.getSubButtons().add(queryButton);
        WxMenuButton scanButton =new WxMenuButton();
        scanButton.setType(WxConsts.MenuButtonType.SCANCODE_PUSH);
        scanButton.setKey("scan_01");
        scanButton.setName("扫码开箱");
        menu.getButtons().add(scanButton);
        WxMenuButton binsButton = new WxMenuButton();
        binsButton.setType(WxConsts.MenuButtonType.VIEW);
        binsButton.setName("附近回收箱");
        binsButton.setUrl( String.format("%s://%s/app/listBins", requestURL.getProtocol(), requestURL.getHost()));
        menu.getButtons().add(binsButton);
        this.wxService.switchover(appid);
        final String menuStr = this.wxService.getMenuService().menuCreate(menu);
        return menuStr;
    }
    
    @PostMapping({ "/createByJson" })
    public String menuCreate(@PathVariable final String appid, @RequestBody final String json) throws WxErrorException {
        return this.wxService.switchoverTo(appid).getMenuService().menuCreate(json);
    }
    
    @GetMapping({ "/delete" })
    public void menuDelete(@PathVariable final String appid) throws WxErrorException {
        this.wxService.switchoverTo(appid).getMenuService().menuDelete();
    }
    
    @GetMapping({ "/delete/{menuId}" })
    public void menuDelete(@PathVariable final String appid, @PathVariable final String menuId) throws WxErrorException {
        this.wxService.switchoverTo(appid).getMenuService().menuDelete(menuId);
    }
    
    @GetMapping({ "/get" })
    public WxMpMenu menuGet(@PathVariable final String appid) throws WxErrorException {
        final WxMpMenu wxMpMenu = this.wxService.switchoverTo(appid).getMenuService().menuGet();
        final String webRootPath = Utils.getRealPath("/config");
        final File menuFile = new File(webRootPath, "menu.json");
        try {
            FileUtils.write(menuFile, (CharSequence)wxMpMenu.toJson(), "utf-8");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return wxMpMenu;
    }
    
    @GetMapping({ "/menuTryMatch/{userid}" })
    public WxMenu menuTryMatch(@PathVariable final String appid, @PathVariable final String userid) throws WxErrorException {
        return this.wxService.switchoverTo(appid).getMenuService().menuTryMatch(userid);
    }
    
    @GetMapping({ "/getSelfMenuInfo" })
    public WxMpGetSelfMenuInfoResult getSelfMenuInfo(@PathVariable final String appid) throws WxErrorException {
        return this.wxService.switchoverTo(appid).getMenuService().getSelfMenuInfo();
    }
}
